package com.zhuss.intention.processor;

import java.util.Date;
import java.util.stream.Collectors;

import com.zhuss.exception.ExceptionCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.intention.intention.CreatePersonalIntentionBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.intention.db.dao.ext.PersonalIntentionDao;
import com.zhuss.intention.db.entity.PersonalIntention;
import com.zhuss.util.JsonUtil;


/**
 * @description: 个人需求创建
 * @author: zhushanshuo
 * @create: Fri Dec 25 09:39:45 CST 2020
 **/
@Component("personalIntentionCreate")
public class PersonalIntentionCreate extends AbstractProcessor<Object, CreatePersonalIntentionBo> implements Processor<Object, CreatePersonalIntentionBo> {

    @Autowired
    private PersonalIntentionDao personalIntentionDao;

    @Autowired
	@Qualifier("personalIntentionRead")
	private PersonalIntentionRead personalIntentionRead;


    /**
     * 校验
     */
    @Override
    public void check(CreatePersonalIntentionBo bo) throws DzcxException {
		if (bo.getRideTypeList().size() == 0) {
			 throw new DzcxException(ExceptionCode.乘坐方式为空);
		}

		if (bo.getTravelPurpose() == null) {
			throw new DzcxException(ExceptionCode.出行目的为空);
		}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(CreatePersonalIntentionBo param) {
    	PersonalIntention record = new PersonalIntention();
    	record.setTravelPurpose(param.getTravelPurpose());
    	record.setLivePlace(param.getLivePlace());
    	record.setLivePlaceLongitude(param.getLivePlaceLongitude());
    	record.setLivePlaceLatitude(param.getLivePlaceLatitude());
    	record.setGetOnLocation(param.getGetOnLocation());
    	record.setGetOnLongitude(param.getGetOnLongitude());
    	record.setGetOnLatitude(param.getGetOnLatitude());
    	record.setGetOffLocation(param.getGetOffLocation());
    	record.setGetOffLongitude(param.getGetOffLongitude());
    	record.setGetOffLatitude(param.getGetOffLatitude());
    	record.setRideDate(param.getRideDate());
    	record.setEstimatedRideTime(param.getEstimatedRideTime());
    	record.setExpectedArrivalTime(param.getExpectedArrivalTime());
    	record.setRideNumber(param.getRideNumber());
    	record.setIntentionType(param.getIntentionType());
    	record.setRideType(param.getRideType());
    	record.setPhone(param.getPhone());
    	record.setRemark(param.getRemark());
    	record.setSort(param.getSort());

		if (param.getRideTypeList() != null) {
			String collect = param.getRideTypeList().stream().collect(Collectors.joining(","));
			record.setRideType(collect);
		}

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	personalIntentionDao.create(record);
    	param.setTargetId(record.getIntentionId());
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(CreatePersonalIntentionBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.需求.key());
    	record.setAction(LogActionEnum.个人需求创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getPhone());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
